package main

import (
	"fmt"
	"sort"
)

func groupAnagrams(strs []string) [][]string {
	mapp := make(map[string][]string)
	for _, word := range strs {
		chars := []byte(word)
		sort.Slice(chars, func(i, j int) bool {
			return chars[i] < chars[j]
		})
		sortWord := string(chars)
		var list []string
		if _, ok := mapp[sortWord]; ok {
			list = mapp[sortWord]
		}
		list = append(list, word)
		//fmt.Println(list)
		mapp[sortWord] = list
	}

	res := make([][]string, 0)
	for _, v := range mapp {
		res = append(res, v)
		//fmt.Println(res)
	}
	return res
}

func main() {
	strs := []string{"eat", "tea", "tan", "ate", "nat", "bat"}
	anagrams := groupAnagrams(strs)
	fmt.Println(anagrams)
}
